package com.sample.tdf.service.impl;

import cn.com.taiji.common.base.BaseTreeServiceImpl;
import cn.com.taiji.common.exception.CommonException;
import com.sample.tdf.domain.Organization;
import com.sample.tdf.domain.OrganizationRepository;
import com.sample.tdf.dto.EmpOrgDTO;
import com.sample.tdf.dto.EmployeeDTO;
import com.sample.tdf.dto.OrgEmpsDTO;
import com.sample.tdf.dto.OrganizationDTO;
import com.sample.tdf.service.IOrganizationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class OrganizationServiceImpl extends BaseTreeServiceImpl<OrganizationRepository, Organization, OrganizationDTO> implements IOrganizationService {
    @Override
    public List<OrgEmpsDTO> findOrgEmpTreeById(String orgId) {
        Organization organization = null;
        //机构不存在
        if (orgId != null) {
            Optional<Organization> byId = iBaseRepository.findById(orgId);
            if (!byId.isPresent()) {
                return new ArrayList<>();
            }
            organization = byId.get();
        }
        //机构存在
        List<EmpOrgDTO> all = iBaseRepository.findAllEmpOrgDTO();
        List<OrganizationDTO> chs = null;
        chs = (orgId == null ? this.domainListToDTOList(iBaseRepository.findAll()) : this.findChildrenByParentId(orgId));
        List<String> chIds = chs.stream().map(dto -> dto.getId()).collect(Collectors.toList());
        all = (orgId == null ? all : all.stream().filter(dto -> chIds.contains(dto.getId())).collect(Collectors.toList()));
        List<OrgEmpsDTO> orgEmpsDTOList = new ArrayList<>();
        for (String id : chIds) {
            OrgEmpsDTO orgEmpsDTO = new OrgEmpsDTO();
            ArrayList<EmployeeDTO> employeeDTOS = new ArrayList<>();
            for (EmpOrgDTO ed : all) {
                if (ed.getId().equals(id)) {
                    orgEmpsDTO.setParentId(ed.getParentId());
                    orgEmpsDTO.setId(id);
                    orgEmpsDTO.setOrgName(ed.getOrgName());
                    if (ed.getEmpId() != null) {
                        EmployeeDTO employeeDTO = new EmployeeDTO();
                        employeeDTO.setId(ed.getEmpId());
                        employeeDTO.setEmpName(ed.getEmpName());
                        employeeDTOS.add(employeeDTO);
                    }
                }
            }
            orgEmpsDTO.setEmpList(employeeDTOS);
            orgEmpsDTOList.add(orgEmpsDTO);
        }
        String rootParentId = organization == null ? null : organization.getId();
        OrgEmpsDTO orgEmps = null;
        String pid = null;
        for (OrgEmpsDTO dto : orgEmpsDTOList) {
            if (rootParentId == null) break;
            if (dto.getId() == rootParentId) {
                orgEmps = dto;
                pid = orgEmps.getParentId();
                orgEmps.setParentId(null);
            }
        }
        List<OrgEmpsDTO> orgEmpsDTOs = this.listToTree(orgEmpsDTOList, null);
        if (orgEmps != null) orgEmps.setParentId(pid);
        return orgEmpsDTOs;
    }
}
